package com.xlf.service.judge.io.impl;

import cn.hutool.core.io.FileUtil;
import com.xlf.common.core.exception.BizException;
import com.xlf.common.core.util.file.UploadConfig;
import com.xlf.pojo.base.ProblemCase;
import com.xlf.pojo.constant.Constants;
import com.xlf.service.judge.io.Resource;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static com.xlf.pojo.constant.Constants.File.TESTCASE_BASE_FOLDER;

/**
 * @Author xlf 2734029211@qq.com
 * @Date 2024/1/27 12:03
 * @Description: TODO 从系统文件夹中读取数据用例，测试用例路径保存在mysql中
 * @Version 1.0
 */
public class FileResource extends MysqlResource {

//    @Deprecated
//    private String base = Constants.JudgeDir.WIN_TEST_CASE_DIR.getContent();

    private String filePath;


    public FileResource(Long problemId) {
        super(problemId);
        String base = UploadConfig.getLocalPath() + TESTCASE_BASE_FOLDER.getPath();
        filePath = base +  File.separator + problemId.toString();
    }

    @Override
    public List<ProblemCase> getCase() {
        List<ProblemCase> aCase = super.getCase();
        //测试用例文件夹
        File testCases = new File(filePath);

        if(!FileUtil.exist(testCases) || testCases.length() == 0)
            throw new BizException(filePath + "：测试用例为空！");

        Map<String, File> inputData = new TreeMap<>();

        Map<String, File> outputData = new HashMap<>();

        for (File tmp : testCases.listFiles()) {
            String tmpPreName = null;
            if (tmp.getName().endsWith(".in")) {
                tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".in"));
                inputData.put(tmpPreName, tmp);
            } else if (tmp.getName().endsWith(".out")) {
                tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".out"));
                outputData.put(tmpPreName, tmp);
            } else if (tmp.getName().endsWith(".ans")) {
                tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".ans"));
                outputData.put(tmpPreName, tmp);
            } else if (tmp.getName().endsWith(".txt")) {
                tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".txt"));
                if (tmpPreName.contains("input")) {
                    inputData.put(tmpPreName.replaceAll("input", "*"), tmp);
                } else if (tmpPreName.contains("output")) {
                    outputData.put(tmpPreName.replaceAll("output", "*"), tmp);
                }
            }
        }

        for (ProblemCase problemCase : aCase) {
            String inputName = problemCase.getInput();
            String outputName = problemCase.getOutput();
            File inputFile = inputData.get(inputName);
            File outputFile = outputData.get(outputName);
            if(inputFile == null || outputFile == null)
                throw new BizException("读取测试用例出错：ID为" + problemCase.getId() + "的测试用例在文件夹中找不到");
            problemCase.setInput(FileUtil.readUtf8String(inputFile));
            problemCase.setOutput(FileUtil.readUtf8String(outputFile));
        }
        return aCase;
    }
}
